package com.example.javaeekeshe.controller;

import com.example.javaeekeshe.pojo.AccountBean;
import com.example.javaeekeshe.pojo.AddressBean;
import com.example.javaeekeshe.pojo.GoodsAndGoodsCarBean;
import com.example.javaeekeshe.service.IAddressService;
import com.example.javaeekeshe.service.IGoodsCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

/**
 * 购物相关 对应buy路径
 */
@Controller
@RequestMapping("/buy")
public class BuyController {
    @Autowired
    IGoodsCarService carService;
    @Autowired
    IAddressService addressService;


    /**
     * 跳转购物车
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/car")
    public String tocar(Model model, HttpSession session){
        //根据用户信息查找购物车
        AccountBean loginUser = (AccountBean) session.getAttribute("loginUser");
        List<GoodsAndGoodsCarBean> goodsAndGoodsCarBeans = carService.selectGoodsCarByUid(loginUser.getId());
        model.addAttribute("cars",goodsAndGoodsCarBeans);
        return "/buy/cart";
    }

    @RequestMapping("/badcar")
    public String tobadcar(Model model, HttpSession session){
        //根据用户信息查找购物车
        AccountBean loginUser = (AccountBean) session.getAttribute("loginUser");
        List<GoodsAndGoodsCarBean> goodsAndGoodsCarBeans = carService.selectGoodsCarByUid(loginUser.getId());
        model.addAttribute("cars",goodsAndGoodsCarBeans);
        model.addAttribute("msg","部分商品已经无货，下单失败");
        return "/buy/cart";
    }

    @RequestMapping("/del/{id}")
    public String del(@PathVariable("id") Integer id,HttpSession session,Model model){
        AccountBean loginUser = (AccountBean) session.getAttribute("loginUser");
        if (!carService.del(id,loginUser.getId())){
            model.addAttribute("msg","删除失败");
        }
        return "redirect:/buy/car";
    }

    /**
     * 支付
     * @return
     */
    @RequestMapping("/pay")
    public String topay(int[] buys,Model model,HttpSession session){
        AccountBean loginUser = (AccountBean) session.getAttribute("loginUser");
        //将传来的信息传递给pay
        if (buys.length>0){
            String gids="";
            String colors="";
            //计算总价格
            BigDecimal sum=new BigDecimal(0);
            //商品列表
            List<GoodsAndGoodsCarBean> buyList = new ArrayList<GoodsAndGoodsCarBean>();
            for (Integer buy:buys) {

                GoodsAndGoodsCarBean goodsAndGoodsCarBean = carService.selectGoodCarByid(buy);
                gids+=goodsAndGoodsCarBean.getGid()+"_";
                colors+=goodsAndGoodsCarBean.getColor()+"_";
                buyList.add(goodsAndGoodsCarBean);
                sum=sum.add(goodsAndGoodsCarBean.getPrice());
            }
            //地址列表
            List<AddressBean> addresses = addressService.selectByUId(loginUser.getId());
            model.addAttribute("addresses",addresses);
            model.addAttribute("buys",buyList);
            model.addAttribute("sum",sum);
            model.addAttribute("gids",gids);
            model.addAttribute("colors",colors);
            return "/buy/pay";
        }else{
            return "/buy/car";
        }

    }
}
